package com.soundShape.mediators.treatment
{
	
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.mediators.AbstractViewMediator;
	import com.soundShape.popUps.AddTreatment;
	import com.soundShape.popUps.EditCustomerCredit;
	import com.soundShape.popUps.EditTreatment;
	import com.soundShape.views.login.Login;
	import com.soundShape.views.treatment.Treatment;
	import com.soundShape.vo.CustomerVO;
	import com.soundShape.vo.TreatmentVO;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.ComboBox;
	import mx.core.UIComponent;
	import mx.effects.Move;
	import mx.effects.Rotate;
	import mx.events.ListEvent;
	import mx.managers.PopUpManager;
	
	import spark.components.BorderContainer;
	
	
	
	
	public class TreatmentMediator extends AbstractViewMediator
	{
		
		
				public var panelTitle:String = "List of Treatments";
				
				public function TreatmentMediator()
				{
					super(Treatment);
				}
				
				/**
				 * Since the AbstractViewMediator sets the view via Autowiring in Swiz,
				 * we need to create a local getter to access the underlying, expected view
				 * class type.
				 */
				public function get view():Treatment
				{
					return this._view as Treatment;
				}
				
				/**
				 * The <code>init()</code> method is fired off automatically by the 
				 * AbstractViewMediator when the creation complete event fires for the
				 * corresponding ViewMediator's view. This allows us to listen for events
				 * and set data bindings on the view with the confidence that our view
				 * and all of it's child views have been created and live on the stage.
				 */
				override protected function init():void
				{
					super.init();
					
					this.setViews();
					
				}
				
				override protected function setViewDataBindings():void
				{
					super.setViewDataBindings();
					
					//BindingUtils.bindProperty(this.view.lblCustomerCredit,"text",this.applicationModel.customerVO,"credit");
					
					BindingUtils.bindProperty(this.view.cmbxCustomers,"dataProvider",this.applicationModel.customers,"source");
					setCustomerComboBox();
				
				}	
				
				override protected function setViewListeners():void
				{
					super.setViewListeners();
					
					//this.view.btnMenu.addEventListener(MouseEvent.CLICK, handleBtnMenu);
					
					this.view.cmbxCustomers.addEventListener(ListEvent.CHANGE, handleCmbxCustomersChange);
					
					//this.view.btnEditCustomerCredit.addEventListener(MouseEvent.CLICK, handleBtnEditCustomerCredit);
					
					this.view.btnAddTreatment.addEventListener(MouseEvent.CLICK, handleBtnAddTreatment);
					
					this.view.dgTreatments.addEventListener(ListEvent.ITEM_CLICK, handleRowClick);
				}
				
				private function setViews():void
				{
					
				}
				
				
				/**
				 * *******************************************************************************
				 * Handlers
				 * *******************************************************************************
				 */
				private function handleBtnMenu(mouseEvent:MouseEvent):void
				{
					//reset view
					if(this.view.dgTreatments.dataProvider != null)
					{
						ArrayCollection(this.view.dgTreatments.dataProvider).removeAll();
						ArrayCollection(this.view.dgTreatments.dataProvider).refresh();
						this.view.btnAddTreatment.enabled = false;
						this.view.cmbxCustomers.selectedIndex = -1;
					}
					
					this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_MENU_VIEW));
				}
				
				private function handleCmbxCustomersChange(listEvent:ListEvent):void
				{
					this.applicationModel.customerVO = ComboBox(listEvent.currentTarget).selectedItem as CustomerVO;
					//this.view.lblCustomerCredit.text = String(this.applicationModel.customerVO.credit);
					this.view.pnTreatmentData.title = "List of Treatments for: " + this.applicationModel.customerVO.name;
					
					//get treatments for this customer
					var soundShapeEvent:SoundShapeEvent = new SoundShapeEvent(SoundShapeEvent.GET_TREATMENTS);
					soundShapeEvent.data = this.applicationModel.customerVO.id;
					this.dispatcher.dispatchEvent(soundShapeEvent);
					
					this.view.btnAddTreatment.enabled = true;
				}
				
				private function handleBtnEditCustomerCredit(mouseEvent:MouseEvent):void
				{
					var editCustomerCredit:EditCustomerCredit = new EditCustomerCredit();
					editCustomerCredit.customerVO = this.applicationModel.customerVO;
					editCustomerCredit.entityManager = this.applicationModel.entityManager;
					//editCustomerCredit.label = this.view.lblCustomerCredit;
					PopUpManager.addPopUp(editCustomerCredit,this.view,true);
					PopUpManager.centerPopUp(editCustomerCredit);
				}
				
				private function handleBtnAddTreatment(mouseEvent:MouseEvent):void
				{
					var addTreatment:AddTreatment = new AddTreatment();
					addTreatment.entityManager = this.applicationModel.entityManager;
					addTreatment.customerVO = this.applicationModel.customerVO;
					addTreatment.employeeID = this.applicationModel.employeeVO.id;
					addTreatment.callback = addTreatmentCallback;
					PopUpManager.addPopUp(addTreatment,this.view,true);
					PopUpManager.centerPopUp(addTreatment);
				}
				
				public function addTreatmentCallback():void
				{
					var soundShapeEvent:SoundShapeEvent = new SoundShapeEvent(SoundShapeEvent.GET_TREATMENTS);
					soundShapeEvent.data = this.applicationModel.customerVO.id;
					this.dispatcher.dispatchEvent(soundShapeEvent);
				}
				
				public function handleRowClick(listEvent:ListEvent):void
				{
					var treatmentVO:TreatmentVO = ArrayCollection(this.view.dgTreatments.dataProvider).getItemAt(listEvent.rowIndex) as TreatmentVO;
					var editTreatment:EditTreatment = new EditTreatment();
					editTreatment.entityManager = this.applicationModel.entityManager;
					editTreatment.customerVO = this.applicationModel.customerVO;
					editTreatment.treatmentVO = treatmentVO;
					editTreatment.callback = editTreatmentCallback;
					PopUpManager.addPopUp(editTreatment,this.view,true);
					PopUpManager.centerPopUp(editTreatment);
				}
				
				public function editTreatmentCallback():void
				{
					var soundShapeEvent:SoundShapeEvent = new SoundShapeEvent(SoundShapeEvent.GET_TREATMENTS);
					soundShapeEvent.data = this.applicationModel.customerVO.id;
					this.dispatcher.dispatchEvent(soundShapeEvent);
				}
				
				
				/**
				 * *******************************************************************************
				 * Swiz EventHanders
				 * *******************************************************************************
				 */
				
				[EventHandler(event="SoundShapeEvent.SET_TREATMENTS")]
				public function setTreatments(soundShapeEvent:SoundShapeEvent):void
				{
					this.view.dgTreatments.dataProvider = soundShapeEvent.data;
				}
				
				[EventHandler(event="SoundShapeEvent.RESET_VIEW_TREATMENT")]
				public function resetTreatmentView(soundShapeEvent:SoundShapeEvent):void
				{
					if(this.view == null)
						return;
					
					this.view.pnTreatmentData.title = "Treatments";
					this.view.dgTreatments.dataProvider = null;
				}
				
				
				/**
				 * *******************************************************************************
				 * util
				 * *******************************************************************************
				 */
				private function setCustomerComboBox():void
				{
					this.view.cmbxCustomers.selectedIndex = -1;
					/*if(this.applicationModel.customerVO != null)
					{
						this.view.cmbxCustomers.selectedIndex = (this.view.cmbxCustomers.dataProvider as ArrayCollection).getItemIndex(this.applicationModel.customerVO);
						addTreatmentCallback();
					}
					else
					{
						this.view.cmbxCustomers.selectedIndex = -1;
					}*/
				}
			
		}
		
	}
